package com.gss.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.gss.armory.domain.BusinessUnitDetailsDTO;
import com.gss.armory.domain.CompanyDetailsDTO;
import com.gss.armory.domain.DepartmentDetailsDTO;
import com.gss.armory.domain.DocGeneralDetailsDTO.DocumentFlowStatus;
import com.gss.armory.domain.DocReviewerApproverForWFDTO;
import com.gss.armory.domain.DocumentCategoryDTO;
import com.gss.armory.domain.DocumentMetaDataAttributesDTO;
import com.gss.armory.domain.DocumentSubCategory;
import com.gss.armory.domain.DocumentTypeDTO;
import com.gss.armory.domain.DocumentWorkflowDetailsDTO.WorkflowStatus;
import com.gss.armory.domain.LocationDetailsDTO;
import com.gss.security.domain.LocationMasterDTO;
import com.gss.security.domain.UserDetailsDTO;

@Component(value="applicationMaps")
public class ApplicationMaps {
	
	public static HashMap<Integer, CompanyDetailsDTO> COMPANYID_COMPANY_MAP = new HashMap<Integer, CompanyDetailsDTO>();
	public static HashMap<String, String> COMPANY_NAME_COMPANY_CODE_MAP = new HashMap<String, String>();
	public static HashMap<Integer, List<LocationDetailsDTO>> COMPANY_LOC_MAP = new HashMap<Integer, List<LocationDetailsDTO>>();
	public static HashMap<Integer, LocationMasterDTO> LOCATION_DETAILS_MAP = new HashMap<Integer, LocationMasterDTO>();
	public static HashMap<String, List<String>> STATE_DISTRICT_MAP = new HashMap<String, List<String>>();
	public static HashMap<String, List<String>> DISTRICT_CITY_MAP = new HashMap<String, List<String>>();
	//public static HashMap<Integer, List<BusinessUnitDetailsDTO> > COMPANYID_BULIST_MAP = new HashMap<Integer, List<BusinessUnitDetailsDTO>>();
	public static HashMap<Integer,Integer> BUID_COMPANYID_MAP = new HashMap<Integer, Integer>();
	public static HashMap<Integer, BusinessUnitDetailsDTO> BUID_BU_MAP = new HashMap<Integer, BusinessUnitDetailsDTO>();
	public static HashMap<String, String> BUTYPE_NAME_DESC_MAP = new HashMap<String, String>();
	public static HashMap<String, String> USERID_USERNAME_MAP = new HashMap<String, String>();
	public static HashMap<String, UserDetailsDTO> USERID_USER_MAP = new HashMap<String, UserDetailsDTO>();
	public static HashMap<String, String> USERID_EMAIL_MAP = new HashMap<String, String>();
	public static HashMap<Long, String> DEPTID_DEPTSNAME_MAP = new HashMap<Long, String>();
	public static HashMap<Long, String>DOCUMENT_ID_NAME_MAP = new HashMap<Long, String>();
	public static HashMap<Long, String> DOCUMENT_ID_CODE_MAP = new HashMap<Long, String>();
	//public static HashMap<Long, List<DepartmentDetailsDTO>> LOCID_DEPTS_MAP = new HashMap<Long, List<DepartmentDetailsDTO>>();
		
	public static final HashMap<Integer, List<DocumentCategoryDTO>> DOC_TYP_DOC_CAT_MAP = new HashMap<Integer, List<DocumentCategoryDTO>>();
	public static final HashMap<Integer, List<DocumentSubCategory>> DOC_CAT_DOC_SUBCAT_MAP = new HashMap<Integer, List<DocumentSubCategory>>();
	public static final HashMap<String,String> SearchViewMap = new HashMap<String, String>();
	public static  HashMap<String, List<Long>> USER_VIEW_GROUP_MAP =  new HashMap<String, List<Long>>();
	public static  HashMap<String, List<Long>> USER_PRINT_GROUP_MAP =  new HashMap<String, List<Long>>();
	public static HashMap<String, List<Long>> USER_ACTIVITY_GROUP_MAP = new HashMap<String, List<Long>>();
	public static HashMap<Integer, String> DOCUMENT_TYPE_ID_NAME_MAP= new HashMap<Integer, String>();
	public static HashMap<Integer, DocumentTypeDTO> DOCUMENT_TYPE_ID_MAP= new HashMap<Integer, DocumentTypeDTO>();//
	public static HashMap<Integer, String> DOCUMENT_CAT_ID_MAP= new HashMap<Integer, String>();//
	public static HashMap<Integer, String> DOCUMENT_SUB_CAT_ID_MAP= new HashMap<Integer, String>();//
	public static List<String> BUTYPE_LIST = new ArrayList<String>();
	
	public static HashMap<Integer, List<DocumentMetaDataAttributesDTO>> DOCUMENT_TYPE_MDATTR_MAP = new HashMap<Integer, List<DocumentMetaDataAttributesDTO>>();//
	public static HashMap<Long, DepartmentDetailsDTO> DEPARTMENTID_DEPARTMENT_MAP = new HashMap<Long, DepartmentDetailsDTO>();
	public static HashMap<Long, LocationDetailsDTO> LOCID_LOC_MAP = new HashMap<Long, LocationDetailsDTO>();
	public static HashMap<Long, String> LOCID_LOCNAME_MAP = new HashMap<Long, String>();
	
	public static HashMap<Long, String> BUID_BUSNAME_MAP = new HashMap<Long, String>();
	public static HashMap<Long, String> BUTYPEID_BUTYPESNAME_MAP = new HashMap<Long, String>();
	
	public static HashMap<Long, List<LocationDetailsDTO>> BUTYPE_BU_MAP = new HashMap<Long, List<LocationDetailsDTO>>();
	public static HashMap<Long, Long> BU_BUTYPE_MAP = new HashMap<Long, Long>();
	public static HashMap<Long, List<DepartmentDetailsDTO>> BU_DEPTS_MAP = new HashMap<Long, List<DepartmentDetailsDTO>>();
	public static HashMap<Long, List<String>> LOC_ID_DEF_USER_LIST_MAP = new HashMap<Long, List<String>>();
	public static HashMap<Integer,DocumentMetaDataAttributesDTO> METADATA_ATTR_ID_ATTR_MAP = new HashMap<Integer, DocumentMetaDataAttributesDTO>();
	public static HashMap<String, String> ROLEID_ROLENAME_MAP = new HashMap<String, String>();
	public static HashMap<String, DocReviewerApproverForWFDTO> ARMORY_REVIEVER_APPROVER = new HashMap<String, DocReviewerApproverForWFDTO>();
	public static HashMap<Enum, Boolean> ARMORY_MASTER_DATA = new HashMap<Enum, Boolean>();
	
	public static HashMap<String, List<Long>> SUPERFICIAL_ROLE_MAP_USERID = new HashMap<String, List<Long>>();
	public static HashMap<String, List<String>> SUPERFICIAL_ROLE_MAP_BUID = new HashMap<String, List<String>>();
	public static HashMap<Enum, String> STATUS_NAME_MAP = new HashMap<Enum, String>();
	
	static {
		STATUS_NAME_MAP.put(WorkflowStatus.PFS, "Pending For Submission");
		STATUS_NAME_MAP.put(WorkflowStatus.PFRS, "Pending For Re-Submission");
		STATUS_NAME_MAP.put(WorkflowStatus.PFR, "Pending For Review");
		STATUS_NAME_MAP.put(WorkflowStatus.PFRR, "Pending For Re-Review");
		STATUS_NAME_MAP.put(WorkflowStatus.PFA, "Pending For Approval");
		STATUS_NAME_MAP.put(WorkflowStatus.PFRW, "Pending For Renewal");
		STATUS_NAME_MAP.put(DocumentFlowStatus.Overdue, "Overdue");
		STATUS_NAME_MAP.put(DocumentFlowStatus.Due, "Due");
		STATUS_NAME_MAP.put(DocumentFlowStatus.Wfip, "Workflow in Progress");
		STATUS_NAME_MAP.put(DocumentFlowStatus.RenewWfip, "Renew Workflow in Progress");
	}
	
	public static HashMap<Long, String> getLocationMapForCurrentUser(){
		HashMap<Long, String> map = new HashMap<Long, String>();
		int companyId =  ApplicationUtilities.getCurrentUserCompanyId();
		System.out.println("inside method(getLocationMapForCurrentUser): company id = "+companyId);
		if(COMPANY_LOC_MAP.containsKey(companyId)){
			List<LocationDetailsDTO> locList =  COMPANY_LOC_MAP.get(companyId);
			if(null != locList && locList.size() > 0){
				for(LocationDetailsDTO loc : locList){
					map.put(loc.getLdId(), loc.getLdName());
				}
			}
		}
		else{
			map.putAll(LOCID_LOCNAME_MAP);
		}
		return map;
	}
	
	public static HashMap<Integer, String> getCompaniesMappedToUser() {
		HashMap<Integer, String> companyOfUserMap = new HashMap<Integer, String>();
		if(null == ((UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getCdId() || 
				ApplicationConstants.ALL_INT.intValue() == ((UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getCdId())
			return companyOfUserMap;
		else {
			int currentUserCompanyId = ApplicationUtilities.getCurrentUserCompanyId();
			CompanyDetailsDTO currentUserCompanyObject = ApplicationMaps.COMPANYID_COMPANY_MAP.get(currentUserCompanyId);
			if(currentUserCompanyObject.getCompanyType().equalsIgnoreCase(ApplicationConstants.GROUP_COMPANY)) {
				companyOfUserMap.put(currentUserCompanyId, ApplicationMaps.COMPANYID_COMPANY_MAP.get(currentUserCompanyId).getCdName());
				for(CompanyDetailsDTO companyObject : ApplicationMaps.COMPANYID_COMPANY_MAP.values()) {
					if(null != companyObject.getParentId() && ApplicationConstants.ALL_INT.intValue() != companyObject.getParentId().intValue()) {
						if(companyObject.getParentId().intValue() == currentUserCompanyId)
							companyOfUserMap.put(companyObject.getCdId(), companyObject.getCdName());
					}
				}
			}
			else {
				companyOfUserMap.put(currentUserCompanyId, ApplicationMaps.COMPANYID_COMPANY_MAP.get(currentUserCompanyId).getCdName());
			}
		}
		return companyOfUserMap;
	}
	
	public static HashMap<Long, String> getLocationMapForReportFilters(){
		HashMap<Long, String> map = new HashMap<Long, String>();
		int companyId =  ApplicationUtilities.getCurrentUserCompanyId();
//		System.out.println("inside method(getLocationMapForCurrentUser): company id = "+companyId);
		if(ApplicationMaps.COMPANYID_COMPANY_MAP.get(companyId).getCompanyType().equalsIgnoreCase(ApplicationConstants.GROUP_COMPANY)) {
			if(COMPANY_LOC_MAP.containsKey(companyId)){
				List<LocationDetailsDTO> locList =  COMPANY_LOC_MAP.get(companyId);
				if(null != locList && locList.size() > 0){
					for(LocationDetailsDTO loc : locList){
						map.put(loc.getLdId(), loc.getLdName());
					}
				}
			}
			for(CompanyDetailsDTO companyObject : ApplicationMaps.COMPANYID_COMPANY_MAP.values()) {
				if(null != companyObject.getParentId() && ApplicationConstants.ALL_INT.intValue() != companyObject.getParentId().intValue()) {
					if(companyObject.getParentId().intValue() == companyId) {
						if(COMPANY_LOC_MAP.containsKey(companyObject.getCdId())){
							List<LocationDetailsDTO> locList =  COMPANY_LOC_MAP.get(companyObject.getCdId());
							if(null != locList && locList.size() > 0){
								for(LocationDetailsDTO loc : locList){
									map.put(loc.getLdId(), loc.getLdName());
								}
							}
						}
					}
				}
			}
		}
		else {
			if(COMPANY_LOC_MAP.containsKey(companyId)){
				List<LocationDetailsDTO> locList =  COMPANY_LOC_MAP.get(companyId);
				if(null != locList && locList.size() > 0){
					for(LocationDetailsDTO loc : locList){
						map.put(loc.getLdId(), loc.getLdName());
					}
				}
			}
			else{
				map.putAll(LOCID_LOCNAME_MAP);
			}	
		}
		
		return map;
	}
	
}
